<?php
// 导入基础类
require_once(dirname(__FILE__) . '/base.class.php');
class Course extends Base
{

    function __construct()
    {
        parent::__construct();
    }

    function index()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        echo json_encode($output);
    }

    /*
        课程列表
    */
    function lists()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        // 获取页码
        if (isset($_GET['page'])) {
            $page = $_GET['page'];
        } else {
            $page = 1;
        }
        // 每页显示数据条数
        if (isset($_GET['page_num'])) {
            $page_num = $_GET['page_num'];
        } else {
            $page_num = 12;
        }
        // 起始位置
        $start = ($page - 1) * $page_num;

        $where=" where 1=1 ";

        if (isset($_GET['cid'])) {
            $cid = $_GET['cid'];
            $where.=" and classify_id={$cid}";
        }

        $sql = "select * from courses $where order by id asc";
        $sql .= " limit {$start},{$page_num}";
        $output['sql']=$sql;
        $list = $this->db->query($sql);
        if (!empty($list)) {
            // 将课程图片处理成绝对路径
            foreach ($list as $key => $item) {
                $list[$key]['cover'] = path($item['cover']);
            }
            // 获取总记录数
            $sql = "select count(id) as count from courses";
            if (isset($cid)) {
                $sql .= " where classify_id={$cid}";
            }
            $res = $this->db->get_one($sql);
            $total = $res['count'];
            $output['data'] = ['list' => $list, 'total' => $total];
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '获取课程失败';
        }

        echo json_encode($output);
    }

    /*
        获取课程详情
    */
    function detail()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($id = $_GET['id']) {
            $sql = "select * from courses where id={$id}";
            $res = $this->db->get_one($sql);
            if ($res) {
                $res['cover'] = path($res['cover']);
                $res['introduce'] = htmlspecialchars_decode($res['introduce']);
                // 更新课程学习次数
                $this->db->query("update courses set hits=hits+1 where id={$id}");
                // 课程信息
                $output['data']['info'] = $res;
                // 获取课程详情列表
                $output['data']['list'] = $this->getCourseItems($id);
            } else {
                $output['errcode'] = 2;
                $output['msg'] = '课程不存在';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = 'params error';
        }
        echo json_encode($output);
    }

    public function getCourseItems($course_id)
    {
        $sql = "select * from course_items where course_id={$course_id}";
        $list = $this->db->query($sql);
        if (is_array($list)) {
            foreach ($list as $key => $item) {
                // 处理图片路径问题
                $list[$key]['cover'] = path($item['cover']);
            }
        }
        return $list;
    }

    // 获取课程播放地址
    public function getPlayUrl()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if(isset($_GET['originUrl'])){
            $origin_url=$_GET['originUrl'];
            $output['playUrl']=parseQQUrl($origin_url);
        }
        echo  json_encode($output);
    }

    /*
        课程推荐
    */
    public function recommend()
    {
        $list = $this->db->query("select * from courses order by hits desc limit 12");
        foreach($list as $key=>$item){
            $list[$key]['cover']=path($item['cover']);
        }
        $output = ['errcode' => 0, 'msg' => 'success'];
        $output['data'] = $list;
        echo  json_encode($output);
    }
    /*
        课程搜索
    */
    function search()
    {
        $output = ['errcode' => 0, 'msg' => 'success'];
        if ($keywords = $_GET['keywords']) {
            $sql = "select * from courses where name like '%{$keywords}%'";
            $list = $this->db->query($sql);
            if (!empty($list)) {
                foreach ($list as $key => $item) {
                    // 处理图片路径问题
                    $list[$key]['cover'] = path($item['cover']);
                }
                $output['data'] = $list;
            } else {
                $output['errcode'] = 2;
                $output['msg'] = '暂无搜索结果';
            }
        } else {
            $output['errcode'] = 1;
            $output['msg'] = '参数错误';
        }
        echo json_encode($output);
    }
}
